<?php
namespace M\Action;
use Common\Model;
class CanvasAction extends BaseAction{
	public function _initialize() {
		parent::_initialize();
		
	}
	

public function index(){
	
        $images = I('pic_url','','trim');
		$images = str_replace('https','http',$images);
		$image = $this->getImage($images);
		if($image){
			exit(json_encode($image));
}
}

public function del_img(){
       $url = I('address','','trim');
		$file = FTX_IMG_DATA_PATH.$url; 
		$result = @unlink($file); 
		exit(json_encode($file));	
}






private function getImage($url,$save_dir='',$filename='',$type=0){
		if(trim($url)==''){
			return array('file_name'=>'','save_path'=>'','error'=>1);
		}
		if(trim($save_dir)==''){
			$save_dir='./';
		}
		if(trim($filename)==''){
			$ext=strrchr($url,'.');
			if($ext!='.gif'&&$ext!='.jpg'&&$ext!='.png'&&$ext!='.jpeg'){
				return array('file_name'=>'','save_path'=>'','error'=>3);
			}
			$filename=time().rand(0,10000).$ext;
		}
		if(0!==strrpos($save_dir,'/')){
			$save_dir.="/" . C("yh_attach_path") . 'avatar/';
		}
		if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
			return array('file_name'=>'','save_path'=>'','error'=>5);
		}
		if($type){
			$ch=curl_init();
			$timeout=10;
			curl_setopt($ch,CURLOPT_URL,$url);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 
		    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
			curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
			$img=curl_exec($ch);
			curl_close($ch);
		}else{
			ob_start();
			readfile($url);
			$img=ob_get_contents();
			ob_end_clean();
		}
		$fp2=@fopen($save_dir.$filename,'a');
		fwrite($fp2,$img);
		fclose($fp2);
		unset($img,$url);
		$save_dir = mb_substr($save_dir, 2);
		return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);
	}
	
	
	
	
	







}